#include <iostream>
#include <pthread.h>
#include <cassert>
#include <unistd.h>
#include <cstdio>

using namespace std;

int a = 0;

//新线程
void* thread_routine(void* args)
{
    const char* name = (const char*)args;
    while(true)
    {
        cout<< "我是新线程，我正在运行！name: "<< name << " a = " << a++ <<endl;
        sleep(1);
    }
}

int main()
{
    //int a = 100;
    pthread_t tid;//线程id

    int n = pthread_create(&tid,NULL,thread_routine,(void*)"thread one");
    //第二个属性为线程属性

    assert(n == 0);

    (void)n;

    //这里是主线程
    while(true)
    {
        char tidbuffer[64];
        snprintf(tidbuffer,sizeof(tidbuffer),"0x%x",tid);
        cout<< "我是主线程，我正在运行！"<< "我的pid是："<< getpid() <<" 我创建出来的线程id是："<< tidbuffer << " a = " << a <<endl;
        sleep(1);
    }

    return 0;
}


